package com.example.myui.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.example.myui.R;
import com.example.myui.utils.DensityUtil;

/**
 * Created by Administrator on 2018/1/17/017.
 */

public class MyProgressBar extends ProgressBar {
    //    默认字体大小
    private static final int DEFAUlT_TEXT_SIZE = 20;
    //    默认字体颜色
    private static final int DEFAUlT_TEXT_COLOUR = R.color.red;
    //    默认未到达颜色
    private static final int DEFAUlT_COLOR_UNREACH = R.color.black;
    //    默认到达字体颜色
    private static final int DEFAUlT_COLOR_REACH = R.color.yellow;
    //    默认未到达字体高度
    private static final int DEFAUlT_HEIGHT_UNREACH = 6;
    //    默认到达高度
    private static final int DEFAUlT_HEIGHT_REACH = 6;
    //    默认间隔
    private static final int getDEFAUlT_TEXT_OFFSET = 10;

    private int mTextSize = DensityUtil.dp2px(DEFAUlT_TEXT_SIZE);
    private int mTextColor = DEFAUlT_TEXT_COLOUR;
    private int mReachColor = DEFAUlT_COLOR_REACH;
    private int mUnReachColor = DEFAUlT_COLOR_UNREACH;
    private int mReachHeight = DensityUtil.dp2px(DEFAUlT_HEIGHT_REACH);
    private int mUnReachHeight = DensityUtil.dp2px(DEFAUlT_HEIGHT_UNREACH);
    private int mOffSet = DensityUtil.dp2px(getDEFAUlT_TEXT_OFFSET);

    private Paint mPaint = new Paint();
    //真正宽度
    private int mRealWidth;

    public MyProgressBar(Context context) {
        this(context, null);
    }

    public MyProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyProgressBar, defStyleAttr, 0);
        mTextSize = (int) typedArray.getDimension(R.styleable.MyProgressBar_progress_text_size_my, DEFAUlT_TEXT_SIZE);
        mTextColor = (int) typedArray.getInt(R.styleable.MyProgressBar_progress_text_color_my, DEFAUlT_TEXT_COLOUR);
        mReachColor = (int) typedArray.getInt(R.styleable.MyProgressBar_progress_reach_color, DEFAUlT_COLOR_REACH);
        mUnReachColor = (int) typedArray.getInt(R.styleable.MyProgressBar_progress_unreach_color, DEFAUlT_COLOR_UNREACH);

        mReachHeight = (int) typedArray.getDimension(R.styleable.MyProgressBar_progress_reach_height, DEFAUlT_HEIGHT_REACH);
        mUnReachHeight = (int) typedArray.getDimension(R.styleable.MyProgressBar_progress_unreach_height, DEFAUlT_HEIGHT_UNREACH);
        mOffSet = (int) typedArray.getDimension(R.styleable.MyProgressBar_progress_text_offset_my, getDEFAUlT_TEXT_OFFSET);

        mPaint.setTextSize(mTextSize);
        mPaint.setAntiAlias(true);

        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthVal = MeasureSpec.getSize(widthMeasureSpec);

        int height = measureHeight(heightMeasureSpec);
        setMeasuredDimension(widthVal, height);
        mRealWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
    }


    @SuppressLint("ResourceAsColor")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(getPaddingLeft(), getHeight() / 2);

        boolean noNeedUnReach = false;
        String text = getProgress() + "%";
        int textWidth = (int) mPaint.measureText(text);

        float radio = getProgress() * 1.0f / getMax();
        float progressX = radio * mRealWidth;
        if (progressX + textWidth > mRealWidth) {
            progressX = mRealWidth - textWidth;
            noNeedUnReach = true;
        }

        float endX = progressX - mOffSet / 2;

        if (endX > 0) {
            mPaint.setColor(mReachColor);
            mPaint.setStrokeWidth(mReachHeight);
            canvas.drawLine(0, 0, endX, 0, mPaint);
        }

        //draw text
        mPaint.setColor(mTextColor);
        int y = (int) -(mPaint.descent() + mPaint.ascent() / 2);
        canvas.drawText(text, progressX, y, mPaint);

        //draw unreachbar
        if (!noNeedUnReach) {
            float start = progressX + mOffSet / 2 + textWidth;
            mPaint.setColor(mUnReachColor);
            mPaint.setStrokeWidth(mUnReachHeight);
            canvas.drawLine(start, 0, mRealWidth, 0, mPaint);
        }

        canvas.restore();
    }

    private int measureHeight(int heightMeasureSpec) {
        int result = 0;
        int modle = MeasureSpec.getMode(heightMeasureSpec);
        int size = MeasureSpec.getSize(heightMeasureSpec);

        if (modle == MeasureSpec.EXACTLY) {
            result = size;
        } else {
            int textHeight = (int) (mPaint.descent() - mPaint.ascent());
            result = getPaddingTop() + getPaddingBottom() -
                    Math.max(Math.max(mReachHeight, mUnReachHeight),
                            Math.abs(textHeight));
            if (modle == MeasureSpec.AT_MOST) {
                result = Math.min(result, size);
            }
        }
        return result;
    }
}
